﻿using System;
using CosyTrade.Data;
using CosyTrade.Store;
using CosyTrade.Trade;
using CosyTrade.UI;
using System.Collections.Generic;
using CosyTrade.Financial;
using DevExpress.XtraEditors.Controls;
using CosyTrade.Organization;

namespace CosyTrade.ClientForm.Trade
{
    public partial class ImportBillDetail : DetailFormBase
    {
        ImportBill CurrentImportBill
        {
            get { return CurrentObject as ImportBill; }
        }

        public ImportBillDetail(Guid importBillKey):base(importBillKey)
        {
            InitializeComponent();            
        }

        public ImportBillDetail(ImportBill bill) : base(bill as BusinessObject)
        {
            InitializeComponent();
        }

        protected override BusinessObject InitBusinessObject(Guid currObjKey)
        {
            return Operation.TradeSystem.GetImportBillDetail(currObjKey);
        }

        protected override void  DataBinding()
        {
            //进口单
            BindingControl(txtExportBillNumber, "EditValue", "ExportBillNumber");
            BindingControl(rdtTallyingDate, "EditValue", "TallyingDate");
            BindingControl(dtCargoDate, "EditValue", "CargoDate");
            BindingControl(rtxtTallyingEmployee, "EditValue", "TallyingEmployee");
            BindingControl(txtReceiveEmployee, "EditValue", "ReceiveEmployee");
            BindingControl(txtPackageCount, "EditValue", "PackageCount");
            BindingControl(cmbImportExchangeRate, "EditValue", "ImportExchangeRate");
            BindingControl(txtTotalCount, "EditValue", "PackageCount");
            BindingControl(txtTotal, "EditValue", "Total");
            txtTotal2.EditValue = CurrentImportBill.Total2;

            //出口单
            BindingControl(txtImTradeCode, "EditValue", "ExportBill_TradeCode");
            BindingControl(txtImCompany, "EditValue", "ReceiveCompany");
            BindingControl(txtImRate, "EditValue", "ExportBill_ExchangeRate");
            BindingControl(txtImFillEmp, "EditValue", "ExportBill_FillBillEmployee");
            BindingControl(txtImPackageCount, "EditValue", "ExportBill_PackageCount");
            BindingControl(txtImProductCount, "EditValue", "ExportBill_TotalCount");
            BindingControl(txtImTotal, "EditValue", "ExportBill_Total");
            BindingControl(txtImTotal2, "EditValue", "ExportBill_Total2");
            BindingControl(txtImShippingDate, "EditValue", "ExportBill_ConsignmentDate");
            BindingControl(txtImReciveDate, "EditValue", "ExportBill_FinishDate");
            BindingControl(txtContainerNum, "EditValue", "MaptoExportBill");

            BindingControl(txtDPackageCount, "EditValue", "DValuePackageCount");
            BindingControl(txtDProductCount, "EditValue", "DValueTotalCount");
            BindingControl(txtDTotal, "EditValue", "DValueTotal");
            txtDTotal2.EditValue = CurrentImportBill.DValueTotal2;

            IList<ExchangeRate> rateLst =
                Operation.FinancialSystem.GetExchangeRateList(CurrentImportBill.ReceiveCompany.PrimaryKey);
            cmbImportExchangeRate.Properties.Items.Clear();
            foreach (ExchangeRate rate in rateLst)
            {
                ImageComboBoxItem item;
                if (rate.PrimaryKey == CurrentImportBill.ImportExchangeRate.PrimaryKey)
                    item = new ImageComboBoxItem(rate.Exchange.ToString(), CurrentImportBill.ImportExchangeRate);
                else item = new ImageComboBoxItem(rate.Exchange.ToString(), rate);
                cmbImportExchangeRate.Properties.Items.Add(item);
            }

            bindingSource1.DataSource = CurrentImportBill.BillItems;
        }

        protected override bool ControlCanEdit(System.Windows.Forms.Control control)
        {
            if (control == groupControl2) return false;
            if (control == groupControl3) return false;
            if (control == dtCargoDate) return false;
            if (control == txtReceiveEmployee) return false;
            if (control == txtTotal2) return false;
            if (control == rdtTallyingDate || control == rtxtTallyingEmployee || control == txtContainerNum) 
                return false;
            return base.ControlCanEdit(control);
        }

        protected override bool ControlCanVisible(System.Windows.Forms.Control control)
        {
            if (control == labTallyingEmployee || control == rtxtTallyingEmployee 
                || control == labTallyingDate || control == rdtTallyingDate)
                return CurrentImportBill.ImportBillStat == ImportBillStat.Finish;
            return base.ControlCanVisible(control);
        }

        protected override bool IsVail()
        {
            if(string.IsNullOrEmpty(txtExportBillNumber.Text))
            {
                Functions.ShowMessageBox("请输入出口单编号");
                return false;
            }
            if(string.IsNullOrEmpty(txtPackageCount.Text))
            {
                Functions.ShowMessageBox("请输入总装箱数量");
                return false;
            }
            if (string.IsNullOrEmpty(txtTotalCount.Text))
            {
                Functions.ShowMessageBox("请输入商品总件数");
                return false;
            }
            if(string.IsNullOrEmpty(txtTotal.Text))
            {
                Functions.ShowMessageBox("请输入美金总介");
                return false;
            }
            if(string.IsNullOrEmpty(txtDPackageCount.Text))
            {
                Functions.ShowMessageBox("请输入出口装箱数差值");
                return false;
            }
            if (string.IsNullOrEmpty(txtDProductCount.Text))
            {
                Functions.ShowMessageBox("请输入出口总数量差值");
                return false;
            }
            if (string.IsNullOrEmpty(txtDTotal.Text))
            {
                Functions.ShowMessageBox("请输入出口(美金)总价差值");
                return false;
            }
            return true;
        }

        private void ImportBillDetail_Load(object sender, EventArgs e)
        {
            if (CurrentImportBill == null) return;

            IList<ExchangeRate> exchangeRates =
                Operation.FinancialSystem.GetExchangeRateList(CurrentImportBill.ReceiveCompany.PrimaryKey);
            if (exchangeRates == null) return;

            foreach (ExchangeRate rate in exchangeRates)
            {
                ImageComboBoxItem item = new ImageComboBoxItem(rate.Exchange.ToString(), rate);
                cmbImportExchangeRate.Properties.Items.Add(item);
                if (rate.PrimaryKey == CurrentImportBill.ImportExchangeRate.PrimaryKey)
                    cmbImportExchangeRate.SelectedItem = item;
            }
        }

        protected override void Save(BusinessObject currentObj)
        {
            switch (CurrentStat)
            {
                case DetailFormCurrentStat.AddStat:
                    break;
                case DetailFormCurrentStat.EditStat:
                    Operation.TradeSystem.UpdateImportBill(CurrentImportBill);
                    break;
                case DetailFormCurrentStat.ViewStat:
                    break;
                default:
                    throw new ArgumentOutOfRangeException();
            }
        }

        private void CustomDisplay_Employee(object sender, CustomDisplayTextEventArgs e)
        {
            Employee emp = e.Value as Employee;
            if (emp != null) e.DisplayText = emp.EmpName;
        }

        private void txtImRate_CustomDisplayText(object sender, CustomDisplayTextEventArgs e)
        {
            ExchangeRate rate = e.Value as ExchangeRate;
            if (rate != null) e.DisplayText = rate.Exchange.ToString();
        }

        private void txtImCompany_CustomDisplayText(object sender, CustomDisplayTextEventArgs e)
        {
            Company cmp = e.Value as Company;
            if (cmp != null) e.DisplayText = cmp.Caption;
        }

        private void btnCaleBill_Click(object sender, EventArgs e)
        {
            CurrentImportBill.Total = 0;
            CurrentImportBill.TotalCount = 0;
            CurrentImportBill.PackageCount = 0;
            foreach (ImportBillItem billItem in CurrentImportBill.BillItems)
            {
                CurrentImportBill.Total += billItem.Price*billItem.TotalCount;
                CurrentImportBill.TotalCount += billItem.TotalCount;
                CurrentImportBill.PackageCount += billItem.PackageCount;
            }
            txtTotal2.EditValue = CurrentImportBill.Total2;

            CurrentImportBill.DValueTotalCount = CurrentImportBill.TotalCount -
                                                 CurrentImportBill.MaptoExportBill.TotalCount;
            CurrentImportBill.DValuePackageCount = CurrentImportBill.PackageCount -
                                                   CurrentImportBill.MaptoExportBill.PackageCount;
            CurrentImportBill.DValueTotal = CurrentImportBill.Total - CurrentImportBill.MaptoExportBill.Total;

            txtDTotal2.EditValue = CurrentImportBill.DValueTotal2;
        }

        private void rpiProduct_CustomDisplayText(object sender, CustomDisplayTextEventArgs e)
        {
            CompanyStore view = e.Value as CompanyStore;
            if (view == null) e.DisplayText = "";
            else e.DisplayText = view.Products.Caption;
        }

        private void txtContainerNum_Properties_CustomDisplayText(object sender, CustomDisplayTextEventArgs e)
        {
            ExportBillBase exportBill = e.Value as ExportBillBase;
            e.DisplayText = exportBill != null ? exportBill.ContainerNumber : "";
        }
    }
}
